package org.example.rental.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.rental.common.Result;
import org.example.rental.common.User;
import org.example.rental.domain.SysUser;
import org.example.rental.service.SysUserService;
import org.example.rental.util.RandomKeyGenerator;
import org.example.rental.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
public class SystemController {




    @Autowired
    SysUserService sysUserService;

    @Autowired
    RedisUtil redisUtil;

    @Autowired
    User userDetail;


    @RequestMapping("/login")
    public Result login(@RequestBody SysUser sysUser){
        System.out.println(sysUser.getUsername());
        System.out.println(sysUser.getPassword());
        SysUser user = sysUserService.getOne(new QueryWrapper<SysUser>().eq("username", sysUser.getUsername()));
        if (user!=null){
            if (sysUser.getPassword()!=null && user.getPassword().equals(sysUser.getPassword())){
                if (user.getStatus() == 0){
                    return Result.succ(500,"用户已被封禁，请联系管理员",null);
                }
                String key = RandomKeyGenerator.generatorSessionKey();
                redisUtil.set(key,sysUser.getUsername(),60*60);
                userDetail.setSysUser(user);
                Map<String,Object> hash = new HashMap<>();
                hash.put("token",key);
                hash.put("user",user);
                return Result.succ(200,"登录成功",hash);
            }else {
                return Result.fail("用户名或密码错误");
            }
        }else {
            return Result.fail("用户名或密码错误");
        }
    }


    @PostMapping("/register")
    public Result register(@RequestBody SysUser sysUser){
        SysUser user = sysUserService.getOne(new QueryWrapper<SysUser>().eq("username", sysUser.getUsername()));
        if (user==null){
            sysUser.setStatus(1);
            sysUser.setType(0);
            sysUserService.save(sysUser);
        }else{
            return Result.succ(500,"用户名已被注册，注册失败",null);
        }

        return Result.succ(200,"注册成功",null);
    }



    @GetMapping("/getUserInfo")
    public Result getUserInfo(){
        return Result.succ(userDetail.getSysUser());
    }

    @GetMapping ("/logout")
    public Result logout(String token){
        redisUtil.del(token);
        userDetail.setSysUser(null);

        return Result.succ("退出成功");
    }









    @PostMapping(value = "/upload")
    @CrossOrigin(origins = "*")
    public Result fileUpload(
            MultipartFile file, HttpServletRequest req) throws FileNotFoundException {
        //获取数据
        //上传文件 ===>  返回一个文件名
        //项目真实路径   拼接上  upload包
        String realPath = "D:\\vue\\rental-vue\\public\\img";
        System.out.println(realPath);

        File f = new File(realPath);
        if (!f.exists())f.mkdirs();
        //获取上传的文件名
        String oldName = file.getOriginalFilename();
        String suffix = oldName.substring(oldName.lastIndexOf("."));
        String name = UUID.randomUUID().toString();
        String newName = name + suffix;
        //上传
        try {
            file.transferTo(new File(realPath+File.separator+newName));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //数据库存储文件名称
        return Result.succ(200,"上传成功",newName);

    }

}
